package com.zhkj.txg.module.order.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.order.adapter.OrderDetailInfoAdapter
import com.zhkj.txg.module.order.adapter.OrderRefundAdapter
import com.zhkj.txg.module.order.vm.RefundViewModel
import com.zhkj.txg.module.product.ui.WatcherActivity
import com.zhkj.txg.utils.ImageUtils
import kotlinx.android.synthetic.main.activity_order_refund_detail.*
import kotlinx.android.synthetic.main.item_order_detail_status.*
import kotlinx.android.synthetic.main.item_order_goods.*

class OrderRefundDetailActivity : ViewModelActivity<RefundViewModel>() {

    companion object {
        private val ORDER_ID = "orderId"
        private val GOODS_ID = "goodsId"
        private val ORDER_GROUP = "ORDER_GROUP"//拼团订单
        private val REFUND_ID = "refundId"
        fun open(
            activity: Activity,
            refundId: Long,
            orderId: Long,
            goodsId: Long,
            requestCode: Int,
            isGroup: Boolean = false
        ) {
            val intent = Intent(activity, OrderRefundDetailActivity::class.java)
                .putExtra(ORDER_ID, orderId).putExtra(GOODS_ID, goodsId)
                .putExtra(REFUND_ID, refundId)
            if (isGroup) {
                intent.putExtra(ORDER_GROUP, isGroup)
            }
            activity.startActivityForResult(intent, requestCode)
        }
    }

    var isGroup: Boolean = false
    override fun setViewModel(): RefundViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleRefundDetail
    override val layoutResId: Int = R.layout.activity_order_refund_detail

    private val pictureAdapter = OrderRefundAdapter().apply {
        setOnItemClickListener { _, _, position ->
            val list = ArrayList<String>()
            list.addAll(data)
            WatcherActivity.open(this@OrderRefundDetailActivity, list, position)
        }
    }
    private val refundConfirmPopupWindow: SimplePopupWindow by lazy {
        SimplePopupWindow(this).setTitle(getString(R.string.confirm_cancel_refund))
            .setContent(getString(R.string.confirm_cancel_refund_hint))
            .setConfirmCallback(object : OnItemClickListener {
                override fun onClick(popupWindow: SimplePopupWindow) {
                    viewModel.refundUndo(AccountManager.getToken(), isGroup)
                    refundConfirmPopupWindow.dismiss()
                }
            })
    }
    private val infoAdapter = OrderDetailInfoAdapter()
    private val requestCodeShippingCode = 1919

    override fun initData() {
        viewModel.entity.refundId = intent.getLongExtra(REFUND_ID, 0)
        viewModel.entity.orderId = intent.getLongExtra(ORDER_ID, 0)
        viewModel.entity.goodsId = intent.getLongExtra(GOODS_ID, 0)
        viewModel.refundDetail(AccountManager.getToken(), isGroup)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == requestCodeShippingCode) {
            viewModel.refundDetail(AccountManager.getToken(), isGroup)
        }
    }

    override fun initViewModel() {
        isGroup = intent.getBooleanExtra(ORDER_GROUP, false)
        // 撤销退款观察者
        viewModel.refundUndoLiveData.observe(this, Observer {
            handlerResponseStatus(it.status, it.statusTip)
            if (it.status == HttpResponseStatus.Status.SUCCESS) {
                toast(it.response?.msg ?: getString(R.string.refund_undo_success))
                setResult(RESULT_OK)
                finish()
            }
        })
        //  获取退款详情观察者
        viewModel.refundDetailLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.data?.let {
                it.goods?.let { goods ->
                    ImageUtils.loadRoundCornerImage(this, ivOrderGoods, goods.image)
                    tvOrderGoodsName.text = goods.goodsName
                    tvOrderGoodsPrice.text = SpanUtil.setMoneySymbol(goods.goodsPrice)
                    tvOrderGoodsNumber.text = "x${goods.goodsNumber}"
                }
                it.refundInfo?.let { info ->
                    rvOrderRefundInfo.visibility = if (info.info.isEmpty()) View.GONE else View.VISIBLE
                    infoAdapter.setNewData(info.info)
                    if (info.images.isNotEmpty()) {
                        tvRefundPictureTitle.visibility = View.VISIBLE
                        pictureAdapter.setNewData(info.images)
                    }
                    tvRefundSend.visibility = if (info.needSend) View.VISIBLE else View.GONE
                    tvOrderDetailStatus.text = when (info.status) {
                        -2 -> {
                            tvRefundUndo.visibility = View.GONE
                            getString(R.string.refund_status_cancel)
                        }
                        -1 -> {
                            tvRefundUndo.visibility = View.GONE
                            getString(R.string.refund_status_deny)
                        }
                        1 -> {
                            tvRefundUndo.visibility = View.VISIBLE
                            getString(R.string.refund_status_pass)
                        }
                        2 -> {
                            tvRefundUndo.visibility = View.VISIBLE
                            getString(R.string.refund_status_shop_send)
                        }
                        3 -> {
                            tvRefundUndo.visibility = View.VISIBLE
                            getString(R.string.refund_status_receive)
                        }
                        4 -> {
                            tvRefundUndo.visibility = View.GONE
                            getString(R.string.refund_status_change_success)
                        }
                        5 -> {
                            tvRefundUndo.visibility = View.GONE
                            getString(R.string.refund_status_refund_success)
                        }
                        else -> {
                            tvRefundUndo.visibility = View.VISIBLE
                            getString(R.string.refund_status_audit)
                        }
                    }
                }
            }
        })
    }

    override fun initUI() {
        with(rvRefundDetail) {
            hasFixedSize()
            layoutManager = GridLayoutManager(this@OrderRefundDetailActivity, 3)
            adapter = pictureAdapter
        }
        tvRefundCustomer.setOnClickListener {
            // 联系客服
//            VP53Manager.getInstance().EnterChatActivity(KF_53_ARG, "1", this, null)
        }
        // 撤销退款
        tvRefundUndo.setOnClickListener {
            refundConfirmPopupWindow.showPopupWindow()
        }
        // 填写单号
        tvRefundSend.setOnClickListener {
            OrderRefundShippingActivity.open(
                this, viewModel.entity.orderId,
                viewModel.entity.goodsId, requestCodeShippingCode, isGroup
            )
        }
        with(rvOrderRefundInfo) {
            layoutManager = LinearLayoutManager(this@OrderRefundDetailActivity)
            adapter = infoAdapter
        }
    }
}